package ru.p03.tvipuskeditor.client.tvipuskeditor;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.ui.Connect;

import ru.p03.tvipuskeditor.TvipuskEditor;
import ru.p03.tvipuskeditor.client.tvipuskeditor.TvipuskEditorWidget;
import ru.p03.tvipuskeditor.client.tvipuskeditor.TvipuskEditorServerRpc;
import com.vaadin.client.communication.RpcProxy;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.client.MouseEventDetailsBuilder;
import ru.p03.tvipuskeditor.client.tvipuskeditor.TvipuskEditorClientRpc;
import ru.p03.tvipuskeditor.client.tvipuskeditor.TvipuskEditorState;
import com.vaadin.client.communication.StateChangeEvent;

@Connect(TvipuskEditor.class)
public class TvipuskEditorConnector extends AbstractComponentConnector {

	TvipuskEditorServerRpc rpc = RpcProxy.create(TvipuskEditorServerRpc.class, this);
	
	public TvipuskEditorConnector() {
		
		registerRpc(TvipuskEditorClientRpc.class, new TvipuskEditorClientRpc() {
			
//			public void alert(String message) {
//				// TODO Do something useful
//				Window.alert(message);
//			}

			//Отправляет Xml текст от клиента на сервер
			@Override
			public void sendGetTextRequest() {
				rpc.fireGetTextEventRpc(getWidget().getText());
			}
   
			@Override
			public void setEndabled(boolean enabled) {
				getWidget().editorText.setEnabled(enabled);
			}
  
			@Override
			public void sendFilename(String filename) {
				getWidget().editorText.setText(
							getWidget().editorText.getText() + "\nfilename: " + filename
						);
			}

			@Override
			public void sendDuration(String duration) {
				getWidget().editorText.setText(
						getWidget().editorText.getText() + "\nDuration: " + duration
					);
			}			
			
			@Override
			public void setTeWidth(String width) {
				//getWidget().setWidth(width);				
				getWidget().editorText.setWidth(width);
			}

			@Override
			public void setTeHeight(String height) {
				getWidget().editorText.setHeight(height);
				
			}

			//Принятый от сервера текст парсит и вставляет в редактор текста
			@Override
			public void setXmlText(String xmlText) {
				getWidget().setXmlText(xmlText);
			}

		});

//		// TODO ServerRpc usage example, do something useful instead
//		getWidget().addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				final MouseEventDetails mouseDetails = MouseEventDetailsBuilder
//					.buildMouseEventDetails(event.getNativeEvent(),
//								getWidget().getElement());
//				rpc.clicked(mouseDetails);
//			}
//		});
//
	}

	@Override
	protected Widget createWidget() {
		return GWT.create(TvipuskEditorWidget.class);
	}

	@Override
	public TvipuskEditorWidget getWidget() {
		return (TvipuskEditorWidget) super.getWidget();
	}

	@Override
	public TvipuskEditorState getState() {
		return (TvipuskEditorState) super.getState();
	}
//
//	@Override
//	public void onStateChanged(StateChangeEvent stateChangeEvent) {
//		super.onStateChanged(stateChangeEvent);
//
//		// TODO do something useful
//		//final String text = getState().text;
//		
//		//getWidget().editorText.setText(text);
//		
//		//Пришел запрос на сохранение посылаем серверу текст
//		
//		//rpc.save();
//	}  

}

